package servlet;

import model.Rating;
import service.RatingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 显示商品评分列表的Servlet
 */
@WebServlet(name = "rating_list", urlPatterns = "/rating_list")
public class RatingListServlet extends HttpServlet {
    private RatingService ratingService = new RatingService();

    /**
     * 处理显示商品评分列表的请求
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取商品ID
        int goods_id = Integer.parseInt(request.getParameter("goodsid"));
        
        // 获取评分列表
        List<Rating> ratingList = ratingService.getRatingsByGoodsId(goods_id);
        
        // 获取平均评分
        double averageScore = ratingService.getAverageScoreByGoodsId(goods_id);
        
        // 获取评分数量
        int ratingCount = ratingService.getRatingCountByGoodsId(goods_id);
        
        // 设置请求属性
        request.setAttribute("ratingList", ratingList);
        request.setAttribute("averageScore", averageScore);
        request.setAttribute("ratingCount", ratingCount);
        request.setAttribute("goods_id", goods_id);
        
        // 转发到评分列表页面
        request.getRequestDispatcher("/rating_list.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
} 